from typing import Any

from binance import helpers
from fastapi import APIRouter, Depends
from loguru import logger
from sqlalchemy.orm import Session

from app.api import deps
from app.common.decorators import responseBody
from app.common.response import ResponseResult
from app.core.celery_app import celery_app
from app.core.constant_code import ConstantItem
from app.dao.instrument_dao import instrumentDao
from app.schemas.instrument import InstrumentQuery
from app.spider.binance.binance_spider import BinanceBarSpider

router = APIRouter()




@router.get("/crawlBinanceCalendar")
@responseBody
async def crawlBinanceCalendar() -> Any:
    logger.info("start crawlBinanceCalendar")
    celery_app.send_task("app.tasks.binance_spider_task.calendarSpider")


@router.get("/crawlBinanceInstrument")
@responseBody
async def crawlBinanceInstrument() -> Any:
    logger.info("start crawlBinanceInstrument")
    celery_app.send_task("app.tasks.binance_spider_task.instrumentSpider")



@router.get("/crawlBinanceBar")
@responseBody
async def crawlBinanceBar(*, db: Session = Depends(deps.getDb)) -> Any:
    # logger.info("start crawlBinanceBar")
    # celery_app.send_task("app.tasks.binance_spider_task.barSpider")
    # queryWrapper = InstrumentQuery(marketType=ConstantItem.MARKET_CRYPTOCURRENCY)
    # instruments = instrumentDao.select(db, queryWrapper)
    # tushareSpider = BinanceBarSpider(db, instruments)
    # tushareSpider.crawlDay()
    # tushareSpider.crawl1hour()

    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_1MIN, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_3MIN, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_5MIN, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_15MIN, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_30MIN, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_1HOUR, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_2HOUR, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_4HOUR, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_6HOUR, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_8HOUR, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_12HOUR, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_1D, '2010-01-01')
    spider.crawl()
    spider = BinanceBarSpider(db, ConstantItem.FREQUENCY_1W, '2010-01-01')
    spider.crawl()

